Macromedia Flex Article

Learning Flex Basics (Part 2): Creating a Simple Calculator with Macromedia Flex


Table of Contents

  1. Introduction
  2. Building the Calculator
  3. Create the Application Object
  4. Add a Panel for the Calculator
  5. Add the Calculator Display
  6. Add a Grid to Lay Out the Buttons
  7. Add the Calculator Buttons
  8. Create the CalculatorController Class
  9. Add Properties to the Controller Class
  10. Add Controller Methods
  11. Instantiate the CalculatorController Class
  12. Complete Code Listing for calculator.mxml
  13. Complete Code Listing for CalculatorController.as
  14. Where to Go From Here

Add a Grid to Lay Out the Buttons

To lay out the calculator buttons, you use a Grid layout container. A Grid container is similar to an HTML table: It arranges information in rows and columns using nested GridRow and GridItem containers. GridItem containers have colSpan and rowSpan properties that work the same as the HTML <td> attributes. You are going to lay out the buttons for the calculator in the grid. If you look at the screen shot of the finished calculator on page 2, you will see that you need five rows, and that the top and bottom rows will contain three buttons, while the middle three rows contain four.

  1. After the Label element, but still inside the Panel, add a Grid container block:

    <mx:Grid>
    </mx:Grid>
    
  2. Inside the Grid container block, add five GridRow container blocks:

    <mx:GridRow></mx:GridRow>
    <mx:GridRow></mx:GridRow>
    <mx:GridRow></mx:GridRow>
    <mx:GridRow></mx:GridRow>
    <mx:GridRow></mx:GridRow>
    
  3. Inside the first GridRow container , add three GridItem containers, and set the colSpan of the first item to 2:

    <mx:GridItem colSpan="2"></mx:GridItem>
    <mx:GridItem></mx:GridItem>
    <mx:GridItem></mx:GridItem>
    
  4. Inside each of the second, third, and fourth GridRow containers, add four GridItem containers:

    <mx:GridItem></mx:GridItem>
    <mx:GridItem></mx:GridItem>
    <mx:GridItem></mx:GridItem>
    <mx:GridItem></mx:GridItem>
    
  5. Inside the fifth GridRow container , add three GridItem containers, and set the colSpan of the third item to 2:

    <mx:GridItem></mx:GridItem>
    <mx:GridItem></mx:GridItem>
    <mx:GridItem colSpan="2"></mx:GridItem>